package com.hr.controller.user;


import com.hr.domain.dto.UserFavoriteDTO;
import com.hr.domain.vo.FavoriteVO;
import com.hr.domain.vo.UserFavoriteVO;
import com.hr.service.FavoriteService;
import com.hr.utils.Result;
import com.hr.utils.UserContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user/favorite")
@Tag(name = "用户收藏")
@AllArgsConstructor
public class FavoriteController {

    private final FavoriteService favoriteService;

    /**
     * 添加收藏
     * @param houseId 房源ID
     * @return Result
     */
    @GetMapping("/add/{houseId}")
    @Operation(summary = "添加收藏")
    public Result addFavorite(@PathVariable("houseId") Long houseId) {
        log.info("添加收藏，houseId: {}", houseId);
        // 获取当前登录用户ID
        Long userId = UserContext.getUserId();
        boolean result = favoriteService.addFavorite(houseId, userId);
        return result ? Result.success("添加成功") : Result.error("添加失败");
    }

    /**
     * 取消收藏
     * @param houseId 房源ID
     * @return Result
     */
    @DeleteMapping("/cancel/{houseId}")
    @Operation(summary = "取消收藏")
    public Result cancelFavorite(@PathVariable("houseId") Long houseId) {
        log.info("取消收藏，houseId: {}", houseId);
        // 获取当前登录用户ID
        Long userId = UserContext.getUserId();
        boolean result = favoriteService.cancelFavorite(houseId, userId);
        return result ? Result.success("取消成功") : Result.error("取消失败");
    }

    /**
     * 获取收藏列表
     * @return Result
     */
    @GetMapping("/list")
    @Operation(summary = "获取收藏列表")
    public Result getFavoriteList() {
        log.info("获取收藏列表");
        List<FavoriteVO> houseIdList = favoriteService.getFavoriteList();
        if (houseIdList == null){
            return Result.error("获取失败");
        }
        return Result.success(houseIdList);
    }
    /**
     * 获取单个用户收藏列表
     * @return Result
     */
    @GetMapping("/oneUserList")
    @Operation(summary = "获取单个用户收藏列表")
    public Result getOneUserFavoriteList(UserFavoriteDTO userFavoriteDTO) {
        log.info("获取单个用户收藏列表");
        List<UserFavoriteVO> houseIdList = favoriteService.getOneUserFavoriteList(userFavoriteDTO);
        if (houseIdList == null){
            return Result.error("获取失败");
        }
        return Result.success(houseIdList);
    }

}
